//
//  YTFToolBarView.swift
//  sinaWeibo
//
//  Created by pro on 16/3/8.
//  Copyright © 2016年 pro. All rights reserved.
//

import UIKit

//定义一个枚举，来表示显示那种表情
enum YTFEmotionToolBarType: Int{
    case Rescent = 1000
    case Default = 1001
    case Emoji = 1003
    case Lxh = 1004
}

//声明一个切换的协议
protocol YTFToolBarViewDelegate: NSObjectProtocol {
    //声明一个方法
    func didToolBarItemClick(type: YTFEmotionToolBarType)
}

class YTFToolBarView: UIView {
    
    //声明一个代理属性
    weak var delegate: YTFToolBarViewDelegate?
    
    //重写构造方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //添加控件并设置约束
    private func setupUI(){
        
        //添加控件
        addSubview(rescentButton)
        addSubview(defaultButton)
        addSubview(emojiButton)
        addSubview(lxhButton)
        
        //设置约束
        //最近
        rescentButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.bottom.equalTo(self.snp_bottom)
            make.width.equalTo(defaultButton.snp_width)
        }
        
        //默认
        defaultButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(rescentButton.snp_right)
            make.bottom.equalTo(self.snp_bottom)
            make.width.equalTo(emojiButton.snp_width)
        }
        
        //emoji
        emojiButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(defaultButton.snp_right)
            make.bottom.equalTo(self.snp_bottom)
            make.width.equalTo(lxhButton.snp_width)
        }
        
        //浪小花
        lxhButton.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(emojiButton.snp_right)
            make.bottom.equalTo(self.snp_bottom)
            make.right.equalTo(self.snp_right)
        }
        
        
    }
    
    //切换表情按钮点击事件
    @objc private func changeEmotion(button: UIButton){
//        print("切换表情")
        
        //取消其他按钮的选中状态，并设置本按钮的选中状态
        for subView in self.subviews{
            
            let btn = subView as! UIButton
            btn.selected = false
        }
        
        button.selected = true
        
        //调用代理方法，实现切换
        delegate?.didToolBarItemClick(YTFEmotionToolBarType(rawValue: button.tag)!)
        
    }
    

    //懒加载
    //最近表情
    private lazy var rescentButton: UIButton = {
        
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        button.setTitle("最近", forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_normal"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_selected"), forState: UIControlState.Selected)
        
        button.addTarget(self, action: "changeEmotion:", forControlEvents: UIControlEvents.TouchUpInside)
        
        //设置标识符
        button.tag = YTFEmotionToolBarType.Rescent.rawValue
        
        return button
    }()
    
    //默认表情
    private lazy var defaultButton: UIButton = {
        
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        button.setTitle("默认", forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_normal"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_selected"), forState: UIControlState.Selected)
        
        button.addTarget(self, action: "changeEmotion:", forControlEvents: UIControlEvents.TouchUpInside)
        
        //设置标识符
        button.tag = YTFEmotionToolBarType.Default.rawValue
        
        return button
    }()
    
    //emoji表情
    private lazy var emojiButton: UIButton = {
        
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        button.setTitle("emoji", forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_normal"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_selected"), forState: UIControlState.Selected)
        
        button.addTarget(self, action: "changeEmotion:", forControlEvents: UIControlEvents.TouchUpInside)
        
        //设置标识符
        button.tag = YTFEmotionToolBarType.Emoji.rawValue
        
        return button
    }()
    
    //浪小花表情
    private lazy var lxhButton: UIButton = {
        
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        button.setTitle("浪小花", forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_normal"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "compose_emotion_table_selected"), forState: UIControlState.Selected)
        
        button.addTarget(self, action: "changeEmotion:", forControlEvents: UIControlEvents.TouchUpInside)
        
        //设置标识符
        button.tag = YTFEmotionToolBarType.Lxh.rawValue
        
        return button
    }()
    
}
